﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace SalePriceSystem
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Controller.AD.LdapAuthentication.GetCurrentUserRole()))
            {
                
                redirectRole(Controller.AD.LdapAuthentication.GetCurrentUserRole().ToLower());
            }
            else
            {
                string ADAuthen = HttpContext.Current.User.Identity.Name;
                string domain = WebConfigurationManager.AppSettings["ADDomainName"] ?? "KCUS";
                domain = domain + "\\";
                if (!String.IsNullOrEmpty(ADAuthen))
                {
                    ADAuthen = ADAuthen.Replace(domain, "");
                    tblUser currentUser = new tblUser();
                    List<tblUser> u = Controller.Helper.Member.GetUserDB(ADAuthen);
                    if (u.Count > 0)
                    {
                        currentUser = u[0];
                        Controller.AD.LdapAuthentication.SetAuthen(currentUser);
                        redirectRole(currentUser.RoleName);
                    }
                    else
                    {
                        Response.Redirect("/page/login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/page/login.aspx");
                }

            }



        }

        private void redirectRole(string roleName)
        {
            switch (roleName)
                {
                    case "sale":
                        Response.Redirect("/page/SaleCustomerManagement.htm");
                        break;
                    case "approver":
                        Response.Redirect("/page/ApprovalPage.htm");
                        break;
                    case "admin":
                        Response.Redirect("/page/AdminDocumentPage.htm");
                        break;
                }
        }
    }
}